﻿Imports System.IO
'
Public Class Form1
    Dim sImportText(999999) As String, sWords(0) As String, sSymbol(100) As String, iCount As Integer, sFileName As String
    Dim iWCount As Integer, iSymbCount As Integer, iWcounter(1) As Integer, iSorter(,) As Integer

    Dim search(100) As String, sint, slast As Integer
    Dim temp_rezume As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sStr As String, sr As StreamReader, iblank As Integer
        OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.CurrentDirectory
        iCount = 0
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            sFileName = OpenFileDialog1.FileName
            sr = New StreamReader(OpenFileDialog1.FileName)
            temp_rezume = sr.ReadToEnd
            sr.Close()
            sr = New StreamReader(OpenFileDialog1.FileName)
            If sFileName.Substring(Len(sFileName) - 3, 3) = "txt" Then
                Do While Not sr Is Nothing
                    sStr = sr.ReadLine
                    If Len(sStr) > 0 Then
                        iCount = iCount + 1
                        sImportText(iCount) = sStr
                        iblank = 0
                    Else
                        iblank = iblank + 1
                        If iblank > 25 Then
                            Exit Do
                        End If
                    End If
                Loop
                sr.Close()
                For i = 0 To iCount
                    Call processing(i)
                Next
                ReDim iWcounter(iWCount)
                ProgressBar1.Maximum = iWCount
                For i = 1 To iWCount
                    If sWords(i) <> "" Then
                        Call count_words(i)
                    End If

                Next
                'MsgBox("d")
                For i = 1 To iWCount
                    If sWords(i) = "" Then
                        For z = i To iWCount
                            sWords(z) = sWords(z + 1)
                            iWcounter(z) = iWcounter(z + 1)
                        Next
                        iWCount = iWCount - 1
                    End If
                Next
                ReDim iSorter(iWCount, iWCount)
                For i = 1 To iWCount
                    iSorter(iWcounter(i), 0) = iSorter(iWcounter(i), 0) + 1
                    iSorter(iWcounter(i), iSorter(iWcounter(i), 0)) = i
                Next


                Call export_value()
                Me.Refresh()
                MsgBox("Требуется перезагрузка")
                Me.Close()

            Else
                TextBox1.Text = sr.ReadToEnd
            End If
        End If
        
    End Sub


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        iSymbCount = 20 ' Количество символов
        sSymbol(1) = "."
        sSymbol(2) = ","
        sSymbol(3) = ":"
        sSymbol(4) = ";"
        sSymbol(5) = Chr(34)
        sSymbol(6) = "'"
        sSymbol(7) = " "
        sSymbol(8) = Chr(9)
        sSymbol(9) = Chr(32)
        sSymbol(10) = Chr(150)
        sSymbol(11) = Chr(160)
        sSymbol(12) = Chr(151)
        sSymbol(13) = "!"
        sSymbol(14) = ","
        sSymbol(15) = "?"
        sSymbol(16) = "["
        sSymbol(17) = "]"
        'MsgBox("(" & sSymbol(32) & ")")
        ComboBox1.Items.Add("asas")
        For i = 100 To 500
            If File.Exists(My.Computer.FileSystem.CurrentDirectory & "\rez\" & i & ".rez") = True Then
                rez_c = rez_c + 1
                Call FileExplorer(i)
            End If
        Next
    End Sub

    Function FileExplorer(ByVal i As Integer)
        Dim sr2 As StreamReader, tstr As String, tempI As Integer
        sr2 = New StreamReader(My.Computer.FileSystem.CurrentDirectory & "\rez\" & i & ".rez")
        tstr = ""
        rez_name(rez_c) = sr2.ReadLine
        Do Until tstr = "------"
            tstr = sr2.ReadLine
            If Len(tstr) > 0 And tstr <> "------" Then

                tempI = tempI + 1
                rez_len(rez_c) = tempI
                rez_word(rez_c, tempI) = tstr.Substring(0, Len(tstr) - 6)
                rez_count(rez_c, tempI) = Int(tstr.Substring(Len(tstr) - 6, 6))
            End If
            ' RichTextBox1.Text = RichTextBox1.Text & rez_word(rez_c, tempI) & "(" & rez_count(rez_c, tempI) & ")"
        Loop
        rezume(rez_c) = sr2.ReadToEnd
        If Len(rez_name(rez_c)) > 0 Then
            ListBox1.Items.Add(rez_name(rez_c))
        Else
            ListBox1.Items.Add("Резюме " & rez_c)
        End If
        list_ind(ListBox1.Items.Count - 1) = rez_c
    End Function


    Function processing(ByVal r As Integer) As String
        Dim iSymb As Integer, sStr As String
        iSymb = 0
        For i = 0 To Len(sImportText(r)) - 1
            If sImportText(r).Substring(i, 1) = " " Then
                'processing = sImportText(r).Substring(iSymb, i - iSymb)
                'Call checkSymb(sImportText(r).Substring(iSymb, i - iSymb))
                iWCount = iWCount + 1
                ReDim Preserve sWords(iWCount)

                sStr = sImportText(r).Substring(iSymb, i - iSymb)
                sStr = checkSymb(sStr)
                sWords(iWCount) = sStr
                iSymb = i + 1
                'MsgBox(sImportText(r) & sWords(iWCount))
            Else
                If i = Len(sImportText(r)) - 1 Then
                    iWCount = iWCount + 1
                    ReDim Preserve sWords(iWCount)

                    sStr = sImportText(r).Substring(iSymb, i - iSymb)
                    'MsgBox(sStr & " (" & checkSymb(sStr) & ")")
                    sStr = checkSymb(sStr)
                    'MsgBox(sImportText(r).Substring(iSymb, i - iSymb) & "---" & sStr)
                    sWords(iWCount) = sStr
                    iSymb = i + 1
                End If
            End If

        Next
    End Function

    Function checkSymb(ByVal s As String) As String
        For i = 0 To Len(s) - 1
            For symb = 1 To iSymbCount
                If Len(s) > i Then
                    'MsgBox(s.Substring(i, 1))
                    If s.Substring(i, 1) = sSymbol(symb) Then
                        'MsgBox(s.Substring(i, 1) & " удалили из " & s)
                        s = s.Remove(i, 1)
                    End If
                End If
            Next
        Next
        checkSymb = s
    End Function

    Function export_value()
        Dim sStr As String, sPath As String, tint As Integer
        sStr = InputBox("Сохранить резюме как:", "Сохранение", "Новое резюме") & vbCrLf
        For i = iWCount To 1 Step -1
            'MsgBox(sWords(i))
            'i = iWCount - i
            For z = 1 To iSorter(i, 0)
                If sWords(iSorter(i, z)) <> "" Then
                    'MsgBox(sWords(iSorter(i, z)))
                    sStr = sStr & sWords(iSorter(i, z)) & iWcounter(iSorter(i, z))
                    'MsgBox(iWcounter(iSorter(i, z)) & "  -   " & Len(Str(iWcounter(iSorter(i, z)))))
                    For g = 0 To 6 - Len(Str(iWcounter(iSorter(i, z))))
                        sStr = sStr & " "
                    Next
                    sStr = sStr & vbCrLf
                End If

            Next
        Next
        If Len(Dir(My.Computer.FileSystem.CurrentDirectory & "\rez", FileAttribute.Directory)) = 0 Then
            MkDir(My.Computer.FileSystem.CurrentDirectory & "\rez")
        End If
        tint = 0
        For i = 100 To 500
            If File.Exists(My.Computer.FileSystem.CurrentDirectory & "\rez\" & i & ".rez") = False And tint = 0 Then
                tint = i
                Exit For
            End If
        Next

        sPath = My.Computer.FileSystem.CurrentDirectory & "\rez\" & tint & ".rez"
        File.Delete(sPath)
        File.WriteAllText(sPath, sStr & vbCrLf & "------" & vbCrLf & temp_rezume)
    End Function

    Function count_words(ByVal c)
        iWcounter(c) = 1
        ProgressBar1.Value = c
        For i = c To iWCount
            If i <> c Then
                If StrConv(sWords(i), VbStrConv.Lowercase) = StrConv(sWords(c), VbStrConv.Lowercase) Then
                    iWcounter(c) = iWcounter(c) + 1
                    'MsgBox(StrConv(sWords(i), 1) & "=" & StrConv(sWords(c), 1) & iWcounter(c))

                    iWCount = iWCount - 1
                    sWords(i) = ""
                    'MsgBox(i)
                    i = i - 1
                    'MsgBox(i)
                End If
            End If
        Next
        'MsgBox(StrConv(sWords(c), 1) & "=" & iWcounter(c))
    End Function

    Private Sub ComboBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.Leave
        'MsgBox("end")

    End Sub
    Function ListModifire()

    End Function
 



    Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
        ComboBox1.DroppedDown = True
        
    End Sub

    Function SearchCombo()

        slast = 0
        sint = 0
        For i = 0 To 100
            search(i) = Nothing
        Next
        For f = 0 To Len(ComboBox1.Text) - 1
            If ComboBox1.Text.Substring(f, 1) = "," Or ComboBox1.Text.Substring(f, 1) = " " Then
                sint = sint + 1
                search(sint) = ComboBox1.Text.Substring(slast, f - slast)
                slast = f + 1
                MsgBox(search(sint))
            Else
                If f = Len(ComboBox1.Text) - 1 And f - slast + 1 > 0 Then
                    sint = sint + 1
                    search(sint) = ComboBox1.Text.Substring(slast, f - slast + 1)
                    slast = f + 1
                    MsgBox(search(sint))
                End If
            End If
        Next

        For i = 1 To rez_c
            rez_combo(i) = 0
            For g = 1 To rez_len(i)
                For r = 1 To sint
                    If StrConv(rez_word(i, g), VbStrConv.Lowercase) = StrConv(search(r), VbStrConv.Lowercase) Then
                        rez_combo(i) = rez_combo(i) + rez_count(i, g)
                    End If
                Next
            Next
            If rez_combo(i) > 0 Then

                ListBox1.Items.Add(rez_name(i) & " - " & rez_combo(i))
                list_ind(ListBox1.Items.Count - 1) = i
            End If
        Next
        'MsgBox()

    End Function


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim sStr As String, sr As StreamReader, iblank As Integer
        File.WriteAllText(My.Computer.FileSystem.CurrentDirectory & "ttext.ttxt", RichTextBox1.Text)
        temp_rezume = RichTextBox1.Text
        iCount = 0
        sFileName = My.Computer.FileSystem.CurrentDirectory & "ttext.ttxt"
        sr = New StreamReader(sFileName)
        If sFileName.Substring(Len(sFileName) - 3, 3) = "txt" Then
            Do While Not sr Is Nothing
                sStr = sr.ReadLine
                If Len(sStr) > 0 Then
                    iCount = iCount + 1
                    sImportText(iCount) = sStr
                    iblank = 0
                Else
                    iblank = iblank + 1
                    If iblank > 25 Then
                        Exit Do
                    End If
                End If
            Loop
            sr.Close()
            For i = 0 To iCount
                Call processing(i)
            Next
            ReDim iWcounter(iWCount)
            ProgressBar1.Maximum = iWCount
            For i = 1 To iWCount
                If sWords(i) <> "" Then
                    Call count_words(i)
                End If

            Next

            For i = 1 To iWCount
                If sWords(i) = "" Then
                    For z = i To iWCount
                        sWords(z) = sWords(z + 1)
                        iWcounter(z) = iWcounter(z + 1)
                    Next
                    iWCount = iWCount - 1
                End If
            Next
            ReDim iSorter(iWCount, iWCount)
            For i = 1 To iWCount
                iSorter(iWcounter(i), 0) = iSorter(iWcounter(i), 0) + 1
                iSorter(iWcounter(i), iSorter(iWcounter(i), 0)) = i
            Next


            Call export_value()
            MsgBox("Требуется перезагрузка")
            Me.Close()
        Else
            TextBox1.Text = sr.ReadToEnd
        End If
        File.Delete(sFileName)
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        'MsgBox(ListBox1.SelectedIndex)
        If ListBox1.SelectedIndex >= 0 Then
            RichTextBox1.Text = rezume(list_ind(ListBox1.SelectedIndex))
        End If

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        For i = 101 To 400
            File.Copy(My.Computer.FileSystem.CurrentDirectory & "\rez\" & 100 & ".rez", My.Computer.FileSystem.CurrentDirectory & "\rez\" & i & ".rez")
        Next
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        ListBox1.Items.Clear()
        If Len(ComboBox1.Text) > 0 Then
            ListBox1.Items.Clear()
            Call SearchCombo()
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        For i = 1 To rez_c
            If Len(rez_name(rez_c)) > 0 Then
                ListBox1.Items.Add(rez_name(i))
            Else
                ListBox1.Items.Add("Резюме " & i)
            End If
            list_ind(ListBox1.Items.Count - 1) = i
        Next
    End Sub
End Class
